<template>
  <section class="account-reg">
    <h1>请先注册</h1>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="reg-form">
      <el-form-item label="手机" prop="phone">
        <el-input placeholder="必填" type="text" v-model="ruleForm.phone" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input placeholder="必填" type="text" v-model="ruleForm.username" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input placeholder="必填" type="text" v-model="ruleForm.email" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input placeholder="必填" type="password" v-model="ruleForm.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="pass_repeat">
        <el-input placeholder="必填" type="password" v-model="ruleForm.pass_repeat" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
import {AccountService} from '@/service/api'

export default {
  data () {
    let validatePassRepeat = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    let validatePhone = (rule, value, callback) => {
      let reg = 11 && /^((13|14|15|17|18)[0-9]{9})$/
      if (!reg.test(value)) {
        callback(new Error('请输入合法的手机号'))
      } else {
        callback()
      }
    }
    return {
      time: 0,
      ruleForm: {
        phone: '',
        username: '',
        email: '',
        pass_repeat: '',
        password: ''
      },
      rules: {
        phone: [
          {required: true, message: '请输入手机号', trigger: 'change'},
          {validator: validatePhone, trigger: 'change'}
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'change'},
          {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'change'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'change'},
          {min: 6, max: 64, message: '长度在 6 到 64 个字符', trigger: 'change'},
          {type: 'email', message: '邮箱格式不合法', trigger: 'change'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'change'},
          {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'change'}
        ],
        pass_repeat: [
          {required: true, message: '请输入密码', trigger: 'change'},
          {validator: validatePassRepeat, trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          return false
        }
        AccountService.register(this.ruleForm)
          .then(res => {
            if (res) {
              this.$message.success('注册成功')
              if (this.$router.currentRoute.query.redirect) {
                this.$router.push({path: this.$router.currentRoute.query.redirect})
              } else {
                this.$router.push({path: '/user'})
              }
            }
          })
      })
    }
  }
}
</script>

<style scoped>
</style>
